<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Auth;
use Mail;

class UsersController extends Controller{

    public function __construct(){
        $this->middleware('auth',[
            'except' => ['show','create','store','index','confirmEmail']
        ]);
        $this->middleware('guest',[
            'only' => ['create']
        ]);
    }

    // 列出所有用户
    public function index(){
        $users = User::paginate(10);
        return view('users.index',compact('users'));
    }

    // 用户注册页面
    public function create(){
        return view('users.create');
    }

    public function show(User $user){
        $statuses = $user->statuses()
                    ->orderBy('created_at','desc')
                    ->paginate(30);
        return view('users.show',compact('user','statuses'));
    }

    public function store(Request $request){
        $this->validate($request,[
            'name' => 'required|max:50',
            'email' => 'required|email|max:255|unique:users,email',
            'password' => 'required|confirmed|min:6',
        ]);
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);
        $this->sendEmailConfirmationTo($user);
        session()->flash('success','验证邮件已经发送到你的注册邮箱上，请注意查收。');
        return redirect('/');
        /*
        Auth::login($user);
        session()->flash('success','欢迎，您将在这里开启一段新的旅程~');
        */
        // return redirect()->route('users.show',['user'=>$user->id]);
    }

    public function edit(User $user){
        $this->authorize('update',$user);
        return view('users.edit',compact('user'));
    }

    public function update(User $user,Request $request){
        $this->validate($request,[
            'name' => 'required|max:50',
            'password' => 'nullable|confirmed|min:6',
        ]);
        $this->authorize('update',$user);
        $data = [];
        $data['name'] = $request->name;

        if($request->password){
            $data['password'] = bcrypt($request->password);
        }

        $user->update($data);

        session()->flash('success','个人资料更新成功!');

        return redirect()->route('users.show',$user->id);
    }

    public function destroy(User $user){
        $this->authorize('destroy',$user);
        $user->delete();
        session()->flash('success','成功删除用户');
        return back();
    }

    // 激活逻辑
    public function confirmEmail($token){
        $user = User::where('activation_token',$token)->firstOrFail();
        $user->activated = true;
        $user->activation_token = null;
        if($user->save()){
            Auth::login($user);
            session()->flash('success','恭喜你，激活成功!');
            return redirect()->route('users.show',[$user]);
        }else{
            session()->flash('warning','操作失败，请稍后重试.');
            return redirect()->route('home');
        }
    }

    // 我的关注
    public function followings(User $user){
        $users = $user->followings()->paginate(30);
        $title = '关注的人';
        return view('users.show_follow',compact('users','title'));
    }

    // 我的粉丝
    public function followers(User $user){
        $users = $user->followers()->paginate(30);
        $title = '粉丝';
        return view('users.show_follow',compact('users','title'));
    }

    // 新增用户发送激活邮件。
    protected function sendEmailConfirmationTo($user){
        $view = 'emails.confirm';
        $data = compact('user');
        $to = $user->email;
        $subject = '感谢注册Sample 应用，请确认你的邮箱';
        Mail::send($view,$data,function($message) use($to,$subject){
            $message->to($to)->subject($subject);
        });
    }
}
